import 'dart:developer' as developer;

import 'dart:ffi';

import 'dart:io';

import 'package:ffi/ffi.dart';

class Log {
  static void info({
    required String tag,
    required String message,
    Exception? exception,
  }) {
    var exceptionStr = exception?.toString();
    if (exceptionStr?.isNotEmpty == true) {
      developer.log("message:$message, exception: $exceptionStr", name: tag);
    } else {
      developer.log(message, name: tag);
    }
  }

  static void printAndroidLogger(String log) {
    DynamicLibrary? trackLib;
    if (Platform.isAndroid) {
      trackLib = DynamicLibrary.open("libtracker-lib.so");
    }
    var func = trackLib
        ?.lookup<NativeFunction<CVoidFunctionStringIntParams>>("androidLogger")
        .asFunction<DartVoidFunctionStringIntParams>();
    String value = log;
    Pointer<Utf8> nativeValue = value.toNativeUtf8();
    func?.call(nativeValue, nativeValue.length);
    calloc.free(nativeValue);
  }
}

typedef CVoidFunctionStringIntParams = Void Function(Pointer<Utf8> str, Int32);
typedef DartVoidFunctionStringIntParams = void Function(Pointer<Utf8> str, int);
